<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var obj = {
            name: '张三',

            isSingle: {
                flag: true,
                type: {
                    time: '10年'
                }
            }
        }
        // console.log(obj)
        // var obj2 = obj 
        // console.log(JSON.stringify(obj))
        // var obj2 = JSON.parse(JSON.stringify(obj)) 
        

        // obj2.name = '李四'
        // console.log(obj, obj2)
        // 递归深复制 递归就是函数的自我调用

        function deepCopy(params) {
            var obj2 = {}
            if(params instanceof Object) {
                for(let key in params) {
                    if(params[key] instanceof Object) {
                       obj2[key] = deepCopy(params[key])
                    } else {
                        obj2[key] = params[key]
                    }
                }
            }

            return obj2
        }

        var obj2 = deepCopy(obj)
        obj2.name = '李四'
        obj2.isSingle.flag = false
        obj2.isSingle.type.time = '20年'
        console.log(obj, obj2)

        // 除了这两种方法 剩下的赋值方法全是浅复制
    </script>
</body>
</html>